package com.tepestech.pfautocard.activity

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.tepestech.pfautocard.R
import com.tepestech.pfautocard.fragment.*
import com.tepestech.pfautocard.util.MainPagerAdapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import kotlinx.android.synthetic.main.content_main.*


class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    lateinit var adapter: MainPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.elevation = 0f
        }
        val list = ArrayList<String>()
        list.add(resources.getString(R.string.navigation_basic))
        list.add(resources.getString(R.string.navigation_skill))
        list.add(resources.getString(R.string.navigation_ability))
        list.add(resources.getString(R.string.navigation_spell))
        list.add(resources.getString(R.string.navigation_item))
        list.add(resources.getString(R.string.navigation_other))
        val fragments = ArrayList<Fragment>()
        fragments.add(BasicFragment())
        fragments.add(SkillFragment())
        fragments.add(AbilityFragment())
        fragments.add(SpellFragment())
        fragments.add(ItemFragment())
        fragments.add(OtherFragment())
        adapter = MainPagerAdapter(supportFragmentManager, fragments, list)
        nav_view.setNavigationItemSelectedListener(this)
        viewpager_main.adapter = adapter
        tab_main.setupWithViewPager(viewpager_main)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_action_selectiong, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_attack ->
                Toast.makeText(this@MainActivity, "Attack!", Toast.LENGTH_SHORT).show()
            R.id.action_defend ->
                Toast.makeText(this@MainActivity, "Defend!", Toast.LENGTH_SHORT).show()
            R.id.action_dice ->
                Toast.makeText(this@MainActivity, "RollDice!", Toast.LENGTH_SHORT).show()
        }
        return true
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.drawer_role_list -> {
                Toast.makeText(this, "角色列表", Toast.LENGTH_SHORT).show()
                startActivity(Intent(this@MainActivity, RoleSelectingActivity::class.java))
            }
            R.id.drawer_setting -> {
                Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show()
            }
            R.id.drawer_donate -> {
                Toast.makeText(this, "捐赠作者", Toast.LENGTH_SHORT).show()
            }
            R.id.drawer_exit -> {
                finish()
            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }

}
